package com.example.wechat.controller;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 测试聊天控制器
 * 专门用于测试文件夹下的聊天页面功能
 */
@Controller
public class TestChatController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 访问测试聊天页面
     */
    @GetMapping("/message/test/chat")
    public String testChat() {
        return "message/test/chat";
    }

    /**
     * 处理测试聊天消息发送
     */
    @MessageMapping("/test/chat.send")
    @SendTo("/topic/test/public")
    public Map<String, Object> sendTestMessage(Map<String, Object> message) {
        System.out.println("=== 收到测试聊天消息 ===");
        System.out.println("发送者: " + message.get("fromUser"));
        System.out.println("消息内容: " + message.get("content"));
        
        // 添加时间戳和消息ID
        message.put("timestamp", LocalDateTime.now().toString());
        message.put("messageId", System.currentTimeMillis());
        message.put("messageType", "text");
        
        System.out.println("发送测试消息到公共频道");
        return message;
    }

    /**
     * 处理用户加入通知
     */
    @MessageMapping("/test/user.join")
    @SendTo("/topic/test/user.status")
    public Map<String, Object> userJoin(Map<String, Object> userInfo) {
        System.out.println("用户加入: " + userInfo.get("username"));
        
        Map<String, Object> response = new HashMap<>();
        response.put("username", userInfo.get("username"));
        response.put("status", "online");
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("action", "join");
        
        return response;
    }

    /**
     * 处理用户离开通知
     */
    @MessageMapping("/test/user.leave")
    @SendTo("/topic/test/user.status")
    public Map<String, Object> userLeave(Map<String, Object> userInfo) {
        System.out.println("用户离开: " + userInfo.get("username"));
        
        Map<String, Object> response = new HashMap<>();
        response.put("username", userInfo.get("username"));
        response.put("status", "offline");
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("action", "leave");
        
        return response;
    }

    /**
     * 处理输入状态
     */
    @MessageMapping("/test/chat.typing")
    @SendTo("/topic/test/typing")
    public Map<String, Object> handleTyping(Map<String, Object> typingInfo) {
        Map<String, Object> response = new HashMap<>();
        response.put("username", typingInfo.get("username"));
        response.put("isTyping", typingInfo.get("isTyping"));
        response.put("timestamp", LocalDateTime.now().toString());
        
        return response;
    }

    /**
     * 发送系统消息
     */
    @MessageMapping("/test/system.message")
    @SendTo("/topic/test/system")
    public Map<String, Object> sendSystemMessage(Map<String, Object> systemMessage) {
        Map<String, Object> response = new HashMap<>();
        response.put("type", "system");
        response.put("content", systemMessage.get("content"));
        response.put("timestamp", LocalDateTime.now().toString());
        
        return response;
    }

    /**
     * 获取在线用户列表
     */
    @MessageMapping("/test/users.list")
    @SendTo("/topic/test/users")
    public Map<String, Object> getUsersList() {
        // 这里可以返回当前在线用户列表
        // 实际应用中应该从Session中获取
        Map<String, Object> response = new HashMap<>();
        response.put("users", new String[]{"测试用户1", "测试用户2"});
        response.put("timestamp", LocalDateTime.now().toString());
        
        return response;
    }
}